package com.accesshollywood.components;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.EnumSet;

import javax.swing.JLabel;

import com.accesshollywood.components.BasicPopupMenu.PopupListener;
import com.accesshollywood.trace.Trace;

public class MultiLabelButton extends JLabel implements HasPopupMenu {
	private String[]		labels;
	private int				currLabelIndex		= 0;
	private Object			actionObject;
	private String			actionClassName		= "";
	private String			actionClassMethod	= "";
	public BasicPopupMenu	popup				= new BasicPopupMenu(this);

	public MultiLabelButton(int fontSize) {
		super("");
		// addActionListener(new CycleAction());
		this.addMouseListener(new CycleAction());
		this.setOpaque(true);
		this.setFont(new Font(this.getFont().getName(), this.getFont().getStyle(), fontSize));
		this.setupPopup();
	}

	public void setModel(String[] labels) {
		this.labels = labels;
		setText(labels[currLabelIndex]);
		this.setHorizontalAlignment(CENTER);
	}

	public int getSelectedIndex() {
		return currLabelIndex;
	}

	public void setSelectedIndex(int newIndex) {
		currLabelIndex = newIndex;
	}

	public String getSelectedItem() {
		return labels[currLabelIndex];
	}

	public void setAction(Object actionObject, String className, String classMethod) {
		this.actionObject = actionObject;
		this.actionClassName = className;
		this.actionClassMethod = classMethod;
	}

	public void doClick() {
		new CycleAction().cycleLabels();
	}

	public class CycleAction implements ActionListener, MouseListener {
		public void cycleLabels() {
			currLabelIndex = (currLabelIndex + 1) % labels.length;
			setText(labels[currLabelIndex]);
			if (!actionClassName.equals("") && !actionClassMethod.equals("")) {
				try {
					Class c = Class.forName(actionClassName);
					Method m = c.getDeclaredMethod(actionClassMethod);
					m.invoke(c.cast(actionObject));

				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			cycleLabels();
		}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			switch(arg0.getButton()){
				case MouseEvent.BUTTON1:
					cycleLabels();
					break;
				case MouseEvent.BUTTON3:
					popup.new PopupListener().maybeShowPopup(arg0);
					break;
			}
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}
	}

	@Override
	public void setupPopup() {
		//popup.popupItems = EnumSet.of(
		//		BasicPopupMenu.POPUP_ITEMS.EDIT
		//		);
		//popup.addMenuItem("Edit", new EditActionListener());
	}
}
